Skip to content

Conversation

stickM4N
Copy link

@stickM4N stickM4N commented Dec 1, 2024

I faced this issue with Pydantic V2 and there is a simple workaround to it by passing the pattern Field parameter as schema extra:

Ex:

class MyClass(SQLModel)
    id: str = Field(unique=True, schema_extra={'pattern': r"^\d{5}$"})

I don't know if this issue was already fixed in another branch, I saw there was a temporary PR already made but closed.

@stickM4N
Copy link
Author

@tiangolo @alejsdev or some other maintainer... could you please review this issue?

@svlandeg svlandeg changed the title Add compatibility for Pydantic V2 regex (renamed param from regex to … 💥 Rename regex to param to provide compatibility with Pydantic V2 Feb 20, 2025
@svlandeg
Copy link
Member

Thanks for the PR! We're going through the backlog of PRs right now, and will get back to you once we've had the time to review this and consider the (breaking) change 🙏

@stickM4N
Copy link
Author

stickM4N commented Feb 22, 2025

@svlandeg Nice to hear you are taking action on this matter. There is remark, the parameter the was renamed between Pydantic v1 and v2 is regex-> pattern, not as listed in the PR.

The suggestion I made in the PR was to keep both versions compatible when upgrading, but there is an alternative, which is not managing the change between them and just provide the proper type hints.

If you want me to update the PR just let me know, IDK which approach is more adequate for the project.

@YuriiMotov

This comment was marked as resolved.

@stickM4N stickM4N changed the title 💥 Rename regex to param to provide compatibility with Pydantic V2 💥 Rename regex to pattern to provide compatibility with Pydantic V2 Aug 8, 2025
@stickM4N

This comment was marked as resolved.

@YuriiMotov
Copy link
Member

YuriiMotov commented Aug 23, 2025

Since we still support regex parameter and just add alias (pattern) and make it working with Pydantic V2, I wouldn't call these changes braking.
So, I would remove the mention of breaking.

Also, I think we should mark regex as deprecated and prioritize pattern over regexp regardless the version of Pydantic.

Still we need to add some test:

  • pass regex and validate valid\invalid value
  • pass pattern and validate valid\invalid value
  • pass both regex and pattern and see that pattern is used
  • don't pass pattern directly, but pass schema_extra={"pattern": r"..."} (workaround that is currently being used to make it working with Pydantic V2) - it should still work

@stickM4N
Copy link
Author

stickM4N commented Aug 28, 2025

Noted @YuriiMotov, I will do that.
Here there is the reference to the removed parameter and error raise associated to it. Here is its implementation.

I would like to propose the following change:

def Field(default, *, ..., **schema_extra)

rather than

def Field(default, *, ..., schema_extra)

@github-actions github-actions bot removed the waiting label Aug 28, 2025
@stickM4N stickM4N force-pushed the main branch 2 times, most recently from a5e8961 to 0b9f96f Compare August 30, 2025 11:29
@YuriiMotov
Copy link
Member

I would like to propose the following change:

def Field(default, *, ..., **schema_extra)

I think this should be done in a separate PR. And I would do it in a different way - I would add pydantic_kwargs and json_schema_extra parameters and deprecate schema_extra.


Could you please address other ideas from this comment?

@stickM4N
Copy link
Author

stickM4N commented Sep 9, 2025

I think this should be done in a separate PR. And I would do it in a different way - I would add pydantic_kwargs and json_schema_extra parameters and deprecate schema_extra.
Fine by me!

Since we still support regex parameter and just add alias (pattern) and make it working with Pydantic V2, I wouldn't call these changes braking. So, I would remove the mention of breaking.

Regarding this, I would still label it as breaking because pydantic.v2 does not support the keyarg and will raise an error if provided. You can check the docs and the implementation for farther details.

Tests

  • pass regex and validate valid\invalid value.
  • pass pattern and validate valid\invalid value.
  • pass both regex and pattern and see that pattern is used -> this is implicitly tested because if its V1 pattern will not work and if its V2 the test will raise an error.
  • don't pass pattern directly, but pass schema_extra={"pattern": r"..."} (workaround that is currently being used to make it working with Pydantic V2) -> kept for compatibility, might be removed in the future

@github-actions github-actions bot removed the waiting label Sep 9, 2025
@YuriiMotov

This comment was marked as resolved.

@stickM4N

This comment was marked as resolved.

@YuriiMotov

This comment was marked as resolved.

@YuriiMotov YuriiMotov added feature New feature or request and removed breaking labels Sep 10, 2025
@YuriiMotov YuriiMotov changed the title 💥 Rename regex to pattern to provide compatibility with Pydantic V2 ✨ Support pattern parameter Sep 10, 2025
@stickM4N

This comment was marked as resolved.

Copy link
Member

@YuriiMotov YuriiMotov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM in general, just one suggestion:

I still think we should deprecate regex parameter and prioritize pattern regardless the version of Pydantic installed.
Let's leave this decision to Sebastian

sqlmodel/main.py Outdated
Comment on lines 397 to 400
if IS_PYDANTIC_V2:
current_schema_extra.update(pattern=pattern or regex)
else:
current_schema_extra.update(regex=regex or pattern)
Copy link
Member

@YuriiMotov YuriiMotov Oct 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think we should deprecate regex parameter and prioritize pattern regardless the version of Pydantic installed.
Let's leave this decision to Sebastian

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would leave it like this, separate but not deprecate any, it is the main purpose of this PR, to support both and after migrating from pydantic v1 to v2 it remains compatible

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just a bit confusing that we will have 2 duplicating parameters.
Pydantic has marked regex as deprecated in V2

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's true, but since sqlmodel supports both versions we should keep compatibility.
Otherwise this PR would be just renaming the parameter

Copy link
Member

@YuriiMotov YuriiMotov Oct 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We support both, but mark old one as deprecated. This way we give users time to update their code bases to use new parameter instead of old one.
Later we will drop regex and only allow pattern regardless the Pydantic version.
I think we should go this way

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Then:

  1. Support both parameters (like it is)
  2. In future versions schema_extra and regex will be removed in order to support just pattern as pydantic have done

Copy link
Member

@YuriiMotov YuriiMotov Oct 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but schema_extra will be just renamed to json_schema_extra and schema_extra will be marked as deprecated

@YuriiMotov YuriiMotov self-requested a review October 7, 2025 19:23
@YuriiMotov
Copy link
Member

YuriiMotov commented Oct 7, 2025

Tests currently fail because new Pydantic version has been released and caused linting errors. See: #1591

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants